package cbfg.rvadapter.demo.select_complex

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cbfg.rvadapter.RVHolder
import cbfg.rvadapter.RVHolderFactory
import cbfg.rvadapter.demo.R
import cbfg.rvadapter.demo.databinding.ItemComplexCommodityBinding
import cbfg.rvadapter.demo.databinding.ItemComplexShopBinding
import cbfg.rvadapter.entity.CommodityItem
import cbfg.rvadapter.entity.ShopItem

/**
 * 添加人：  Tom Hawk
 * 添加时间：2018/7/24 9:45
 * 功能描述：
 *
 * 修改人：  Tom Hawk
 * 修改时间：2018/7/24 9:45
 * 修改内容：
 */
class ComplexVHFactory : RVHolderFactory() {

    override fun createViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup?,
        viewType: Int,
        itemClazz: Class<*>?
    ): RVHolder<out Any> {
        val isShopItem = itemClazz == ShopItem::class.java
        return if (isShopItem) ShopItemVH(
            inflater.inflate(
                R.layout.item_complex_shop,
                parent,
                false
            )
        )
        else CommodityItemVH(inflater.inflate(R.layout.item_complex_commodity, parent, false))
    }

    /**
     * 商店 ViewHolder
     */
    private class ShopItemVH(itemView: View) : RVHolder<ShopItem>(itemView) {
        private val binding = ItemComplexShopBinding.bind(itemView)

        override fun setContent(item: ShopItem, payload: Any?) {
            binding.ivShopImage.setImageResource(item.image)
            binding.tvShopName.text = item.name
        }

        override fun onSelectedState(item: ShopItem, selected: Boolean) {
            binding.cbToggleSelectAll.isChecked = selected
        }
    }

    /**
     * 商品 ViewHolder
     */
    private class CommodityItemVH(itemView: View) : RVHolder<CommodityItem>(itemView) {
        private val binding = ItemComplexCommodityBinding.bind(itemView)

        override fun setContent(item: CommodityItem, payload: Any?) {
            binding.ivImage.setImageResource(item.image)
            binding.tvName.text = item.name
            binding.tvPrice.text = "¥ ${String.format("%.2f", item.price)}"
        }

        override fun onSelectedState(item: CommodityItem, selected: Boolean) {
            itemView.isSelected = selected
            binding.cbSelect.isChecked = selected
        }
    }
}